// 类型注解( Type Annotations ) 告知 TSC 变量的类型是什么

// Type Annotations on Variables
let id:string ;
// 仅能为 id 赋予 string 值
id = '123' ;
id = id.padStart( 5 , '0' );
console.log( id );

// Parameter Type Annotations
function sum( numbers: number[] ) {
    // let total = 0 ;
    // numbers.forEach( n => total += n );

    // Array.prototype.reduce( callback(accumulator, currentValue[, index[, array]])[, initialValue] )
    let total = numbers.reduce( (acc,current)=> acc + current , 0 );
    console.log( numbers.join( '+' ) , '=' , total );
}

console.log( sum( [ 1 , 2 , 3 , 4 , 5 , 6 ] ) );

// Return Type Annotations
function add( first: any , second: any ): number {
    if( typeof first === 'number' && typeof second === 'number' )
    return first + second ;
}

// let result = add( '你好','开发喵' );
let result: number = add( 321 , 123 );
console.log( result , typeof result );


// 类型推断 ( type inference ) 是 TSC 确定变量类型的过程
// 如果 TSC 可以推断 变量、参数、返回值 的类型，则没有必要为它们作类型注解

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof
// https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof#typeof_null